<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">hello管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-button type="primary" round @click="param.phonedialogVisible = true">手机号找回</el-button>&nbsp;
                    <i v-if="!isemail">您还未绑定邮箱</i>
                    <el-button type="success" round v-if="isemail" @click="param.emaildialogVisible = true">邮箱找回</el-button>
                    
                    
                </el-form-item>            
                <!-- 手机找回提示框 -->
                <el-dialog
                title="手机号找回"
                :visible.sync="param.phonedialogVisible"
                width="30%"
                :before-close="handleClose">
                <!-- 内容 -->
                <p>您的手机为{{param.phone}}</p>
                <el-input
                    placeholder="请输入您的手机号"
                    v-model="phone"
                    clearable>
                </el-input> 
                <el-button type="primary" round @click="send_code()">发送验证码</el-button>
                <el-input
                    placeholder="请输入验证码"
                    v-model="phonecode"
                    clearable>
                </el-input>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="phonefind">确定</el-button>
                    <el-button type="primary" @click="this.param.phonedialogVisible = false">取消</el-button>
                </span>
                </el-dialog>
                <!-- 邮箱找回提示框 -->
                <el-dialog
                title="邮箱找回"
                :visible.sync="param.emaildialogVisible"
                width="30%"
                :before-close="handleClose">
                <p>您的邮箱为{{param.email}}</p>
                <el-input
                    placeholder="请输入您的邮箱"
                    v-model="email"
                    clearable>
                </el-input> 
                <el-button type="primary" round @click="seng_email()">发送验证码</el-button>
                <el-input
                    placeholder="请输入验证码"
                    v-model="emailcode"
                    clearable>
                </el-input>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="emailfind">确定</el-button>
                    <el-button type="primary" @click="this.param.emaildialogVisible = false">取消</el-button>
                </span>
                </el-dialog>
                <!-- 修改密码 -->
                <el-dialog
                title="请修改您的密码"
                :visible.sync="param.changedialogVisible"
                width="30%"
                :before-close="handleClose">
                <el-input
                    placeholder="请输入您想要修改的密码"
                    v-model="param.password"
                    clearable>
                </el-input>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="change">确定</el-button>
                    <el-button type="primary" @click="this.param.changedialogVisible = false">取消</el-button>
                </span>
                </el-dialog>


                <a class="login-tips" href="/reg" >注册</a>&nbsp;
                <a class="login-tips" href="/login" >登录</a>&nbsp;
            </el-form>
        </div>
    </div>
</template>

<script>
import {Send_find,Get_email,Find_qery,Change_password,Send_email,Email_qery} from '../../api/api.js'
export default {
    data: function() {
        return {
            param: {
                username:this.$route.query.username,
                password:"",
                phonedialogVisible:false,
                emaildialogVisible:false,
                phone:"",
                email:"",
                changedialogVisible:false,
                
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                
            },
            isemail:true,   //是否展示邮箱
            phone:"",
            phonecode:"",
            email:"",
            emailcode:"",
            
        };
    },
    methods: {
        // 发送邮箱
        seng_email(){
            let params = {
                username:this.param.username,
                email:this.email
            }
            
            this.$message('发送中请稍后')
            Send_email(params).then(res=>{
                if(res.data.code == 200){
                    this.$message(res.data.message)
                
            }else{
                this.$message(res.data.message)
                
            }
            })
        },
        // 修改密码
        change(){
            let params = {
                username:this.param.username,
                password:this.param.password
            }
            console.log(params)
            Change_password(params).then(res=>{
                if(res.data.code == 200){
                    this.$message(res.data.message)
                    this.$router.push('/login')
                
                }else{
                    this.$message(res.data.message)
                
            }
            })
        },
        // 手机发送code
        phonefind(){
            let params = {
                username:this.param.username,
                code:this.phonecode
            }
            Find_qery(params).then(res=>{
                this.param.phonedialogVisible = false
                this.param.changedialogVisible = true
            })
        },
        // 邮箱发送code
        emailfind(){
            let params = {
                username:this.param.username,
                code:this.emailcode
            }
            Email_qery(params).then(res=>{
                console.log(res)
                this.param.emaildialogVisible = false
                this.param.changedialogVisible = true
            })
        },
        // 发送短信
        send_code(){
            let params = {
                username:this.param.username,
                phone:this.phone
            }
            this.$message('发送中请稍后')
            Send_find(params).then(res=>{
                if(res.data.code == 200){
                    this.$message(res.data.message)
                
            }else{
                this.$message(res.data.message)
                
            }
            })
        },
    },
    mounted(){
        let params = {
                username:this.param.username,
            }
        Get_email(params).then(res=>{
            if(res.data.code == 200){
                var tel = res.data.phone;
                tel = "" + tel;
                var ary = tel.split("");
                ary.splice(3,4,"****");
                var tel1=ary.join("");
                this.param.phone = tel1
                
                var te2 = res.data.email;
                te2 = "" + te2;
                var ary2 = te2.split("");
                ary2.splice(3,4,"****");
                var tel2=ary2.join("");
                this.param.email = tel2
                console.log(this.param.email)
                if(this.param.email == '****'){
                    this.isemail = false
                }
            }else{
                this.$message(res.data.message)
                this.$router.push('/nopassword')
            }
        })
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/hello.gif);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>